import { Component, Input, } from '@angular/core';


// 工具
import { compareProps } from '../../utils/basic';
/** 借款进度列表
 */
@Component({
  selector: 'progress-list',
  templateUrl: 'progress-list.html'
})
export class ProgressListComponent {

  @Input() options: any= {}; // 接受参数

  // 关联数据
  private progress: Array<any>= [];
  // 状态
  private state: number;
  constructor() {}

  ngOnChanges(){
    if(this.options.progress.length< 1) return;
    this.initInputList();
  }
  /*** 处理后台返回数据,映射到picker中
   * */
  private initInputList(): void
  {
    const backTime= new Date(this.options.time).getTime();
    const oldArr= [...this.options.progress];
    const arr= oldArr.sort(compareProps('order')).reverse();
    if(arr.length===4 && arr[0].timeLimit && arr[0].loanTime)
    {
      const borrowTime= new Date(arr[0].loanTime).getTime();
      let subDay= String((backTime-borrowTime)/(1000 * 60 * 60 * 24));
      const resDay= parseInt(arr[0].timeLimit)- parseInt(subDay);
      if(resDay<0){ // 逾期状态
        arr[0].title= '已逾期';
        arr[0].describe= `你已逾期${-resDay}天,请尽快还款。`;
        arr[0].date= new Date();
      }else{
        arr[0].describe= arr[0].describe.replace('$',resDay);
      }
    }
    this.progress= arr.map(value=> {
      value.date= new Date(value.date);
      return value;
    });
    this.state= this.options.borrowModalStatus || 0;
  }
}
